﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using TA.Turbo.Design;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Outputs a text label, which is optionally linked to a focusable control.
	/// </summary>
	public class Label : BaseSimpleValueControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public Label()
		{
			Tag = HtmlTextWriterTag.Label;
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseValueDisplayControl._clientProperties,
			new BasePropertyDefinition[] {
				new ControlIdPropertyDefinition("For", "F", null) { IsUsedByClient = false, },
				new PropertyDefinition<string>("Text", "Tx", null) { IsUsedByClient = false, },
			});
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		[DefaultValue(HtmlTextWriterTag.Label)]
		public override HtmlTextWriterTag Tag
		{
			get
			{
				return base.Tag;
			}
			set
			{
				base.Tag = value;
			}
		}
		/// <summary>
		/// Gets or sets the text to be displayed.
		/// </summary>
		[Category("Appearance")]
		public string Text
		{
			get
			{
				return GetProperty<string>("Text");
			}
			set
			{
				ViewState["Text"] = value;
			}
		}
		/// <summary>
		/// The ID of the focusable control to be selected when this label is clicked.
		/// </summary>
		[IDReferenceProperty]
		[TypeConverter(typeof(ControlIdConverter<FocusableControlIdLister>))]
		[DefaultValue("")]
		[MergableProperty(false)]
		public string For
		{
			get
			{
				return GetProperty<string>("For");
			}
			set
			{
				ViewState["For"] = value;
			}
		}
		/// <summary>
		/// Non-typed value object.
		/// </summary>
		public override object GenericValue
		{
			get
			{
				return this.Text;
			}
			set
			{
				this.Text = (string)value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region PROPS
		/// <summary>
		/// Gets or sets the <see cref="Text"/> property.
		/// </summary>
		protected internal override string SerializedValue
		{
			get
			{
				return Text;
			}
			set
			{
				Text = value;
			}
		}
		#endregion
		#region METHODS
		private Control GetFocusableControl(Control forControl)
		{
			TControl bc = forControl as TControl;
			if (bc != null)
			{
				if (bc.CanReceiveFocus)
					return bc;
			}
			foreach (Control c in forControl.Controls)
			{
				Control c2 = GetFocusableControl(c);
				if (c2 != null) return c2;
			}
			return null;
		}
		#region INIT
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the "for" attribute.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (!string.IsNullOrEmpty(For))
			{
				Control forControl = this.FindControlGlobal(For);
				if (forControl != null && (forControl = forControl = GetFocusableControl(forControl)) != null)
				{
					writer.AddAttribute(HtmlTextWriterAttribute.For, forControl.ClientID);
				}
			}
			base.AddAttributesToRender(writer);
		}
		/// <summary>
		/// Doesn't add attributes (because this is a label).
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddValueAttributes(HtmlTextWriter writer)
		{
		}
		/// <summary>
		/// Renders the <see cref="Text"/> property.
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderContents(HtmlTextWriter writer)
		{
			base.RenderContents(writer);

			writer.Write(Text);

			if (this.get_IsDesignMode())
			{
				if (string.IsNullOrEmpty(this.Text))
					writer.Write(this.GetType().Name);
			}
		}
		#endregion
		#endregion
		#endregion
	}
}